import { _decorator, Component, Node,
    Vec3,
    v3,
    Mat4,
    mat4,
    quat,
    Quat
} from 'cc';
const { ccclass, property } = _decorator;


const v3_1 = v3();
const v3_2 = v3();
const mat_1 = mat4();
const mat_2 = mat4();
const quat_1 = quat();

@ccclass('CameraFollow')
export class CameraFollow extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;


    @property({type:Node})
    nd_target:Node = null;

    // @property
    // smoothSpeed = 0.125;

    offset = v3();
    degressOffset = v3();

    // @property(Vec3)
    // lookAtOffset = v3();

    start () {
        // Your initialization goes here.
        Vec3.subtract(this.offset,this.node.position,this.nd_target.position);

        // 将相机到player坐标系 下的矩阵M
        // Mat4.invert(mat_1,this.node.worldMatrix);
        // Mat4.copy(mat_1,this.node.worldMatrix);

        Vec3.subtract(this.degressOffset,this.node.forward,this.nd_target.forward);
    }

    update (deltaTime: number) {
        // this.nd_target.getWorldPosition(v3_1);
        // Vec3.add(v3_2,v3_1,this.offset);
        //
        // // Vec3.lerp(v3_2,this.node.position,v3_2,this.smoothSpeed);
        // this.node.setWorldPosition(v3_1);
        //
        // //
        // // Vec3.add(v3_1,v3_1,this.lookAtOffset);
        // this.node.lookAt(v3_1);
        //
        // // Vec3.add(v3_1,this.nd_target.position,this.offset);
        // // this.node.setPosition(v3_1);

        // return;
        // let playerMat = this.nd_target.worldMatrix;
        // Mat4.multiply(mat_2,mat_1,playerMat);
        //
        // Mat4.getTranslation(v3_1,mat_2);
        // Mat4.getRotation(quat_1,mat_2);
        //
        // this.node.setWorldPosition(v3_1);
        // this.node.setWorldRotation(quat_1);

        // Vec3.add(v3_2,this.nd_target.forward,this.degressOffset);
        // this.node.forward = v3_2;
        //
        // Vec3.add(v3_1,this.nd_target.position,this.offset);
        // this.node.setPosition(v3_1);
        return;
    }
}
